<?php

namespace App\Admin\Controllers;

use App\Models\Article;
use App\Models\ArticleTag;
use App\Models\Category;
use App\Models\Tag;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use HyperDown\Parser;
use Illuminate\Http\Request;

class ArticleController extends AdminController
{
	use HasResourceActions;
	
	public function index(Content $content)
	{
		return $content
			->header(trans('admin.article'))
			->description(trans('admin.list'))
			->body($this->grid()->render());
	}
	
	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid()
	{
		$grid = new Grid(new Article());
		
		$grid->id('ID')->sortable();
		$grid->category()->name('分类');
		$grid->title('标题');
		$grid->click('点击量');
		$grid->cover('封面')->image();
		$states = [
			'on'  => ['value' => 1, 'text' => '正常'],
			'off' => ['value' => 0, 'text' => '无效'],
		];
		$grid->is_top('置顶')->switch($states);
		
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
		});
		
		$grid->tools(function (Grid\Tools $tools) {
			$tools->batch(function (Grid\Tools\BatchActions $actions) {
				$actions->disableDelete();
			});
		});
		$grid->filter(function ($filter) {
			$filter->column(1 / 2, function ($filter) {
				$filter->where(function ($query) {
					
					$query->whereHas('user', function ($sub_query) {
						$sub_query->where('name', 'like', "%{$this->input}%");
					})->orWhere('content', 'like', "%{$this->input}%");
				}, '关键词')->placeholder('关键词(包括内容,用户名)');
				$filter->equal('category_id')->select(Category::all()->pluck('name', 'id'));
			});
			
			$filter->column(1 / 2, function ($filter) {
				$filter->gt('created_at')->datetime();
				$filter->between('updated_at')->datetime();
				$filter->equal('status')->radio([
					                                1 => '正常',
					                                0 => '关闭',
				                                ]);
			});
		});
		return $grid;
	}
	
	/**
	 * Create interface.
	 *
	 * @return Content
	 */
	public function create(Content $content)
	{
		return $content
			->header(trans('admin.article'))
			->description(trans('admin.create'))
			->body($this->form());
	}
	
	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	public function form()
	{
		$Article = new Article();
		$form    = new Form($Article);
		
		$form->display('id', 'ID');
		
		$form->select('category_id', '分类')->options(Category::all()->pluck('name', 'id'))->rules('required');
		$form->text('keywords', '关键词');
		$form->textarea('description', '描述');
		$form->listbox('tag', '标签')->options(Tag::all()->pluck('name', 'id'));
//		$form->checkbox('tag_id', '标签')->options(Tag::all()->pluck('name', 'id'));
		$form->image('cover', '封面')->name(function ($file) {
			return 'cover/' . date('Ymd', time()) . '/' . md5(uniqid()) . '.' . $file->guessExtension();
		})->creationRules('required');
		$form->editormd('markdown');
		$form->hidden('template')->default(rand(1,4));
		
		$form->saving(function (Form $form) {
			//markdown转html
			$form->html = (new Parser())->makeHtml($form->markdown);
			if (!$form->description) {
				$description = strip_tags($form->html);
				//使用mb_substr截取中文
				
				$form->description = mb_substr($description, 0, 200);
			}
		});
		
		$form->display('created_at', trans('admin.created_at'));
		$form->display('updated_at', trans('admin.updated_at'));
		
		return $form;
	}
	
	/**
	 * Edit interface.
	 *
	 * @param $id
	 *
	 * @return Content
	 */
	public function edit($id, Content $content)
	{
		$this->is_add = false;
		return $content
			->header(trans('admin.article'))
			->description(trans('admin.edit'))
			->body($this->form()->edit($id));
	}
	
	/**
	 * Show interface.
	 *
	 * @param mixed   $id
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function show($id, Content $content)
	{
		return $content
			->header(trans('admin.article'))
			->description(trans('admin.detail'))
			->body($this->detail($id));
	}
	
	public function uploadImage(Request $request)
	{
		$returnArr = [
			"success" => 0,
			"message" => "未上传文件",
		];
		if ($request->hasFile('editormd-image-file')) {
			$res = uploadFile("editormd-image-file", "article", array("jpg", "jpeg", "png", "gif"));
			if ($res['status_code'] == 200) {
				$returnArr['success'] = 1;
				$returnArr['url']     = $res['url'];
			}
			$returnArr['message'] = $res['message'];
		}
		return response()->json($returnArr);
	}
}
